package com.ysd.lis.service.qi.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.qi.QiIndexItem;
import com.ysd.lis.entity.qi.QiIndexItemFixed;
import com.ysd.lis.entity.qi.QiIndexMain;
import com.ysd.lis.entity.qi.QiIndexMajor;
import com.ysd.lis.mapper.qi.QiIndexItemFixedMapper;
import com.ysd.lis.mapper.qi.QiIndexMajorMapper;
import com.ysd.lis.request.qi.QiIndexItemFixedDto;
import com.ysd.lis.service.qi.QiIndexItemFixedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 指标项目条件表 服务实现类
 * </p>
 *
 * @author PRS
 * @since 2024-08-15
 */
@Service
public class QiIndexItemFixedServiceImpl extends ServiceImpl<QiIndexItemFixedMapper, QiIndexItemFixed> implements QiIndexItemFixedService {

    @Autowired
    QiIndexMajorMapper qiIndexMajorMapper;
    @Autowired
    QiIndexItemFixedMapper qiIndexItemFixedMapper;


    @Override
    public Result findQiIndexItemFixedList(QiIndexItemFixed param) {
        MPJLambdaWrapper<QiIndexMajor> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAs("fi",QiIndexItemFixed::getId,QiIndexItemFixed::getId)
                .selectAs("fi",QiIndexItemFixed::getFixedValue,QiIndexItemFixed::getFixedValue)
                .selectAs("ma",QiIndexMain::getId,QiIndexItemFixed::getIndexId)
                .selectAs("ma",QiIndexMain::getIndexCode,QiIndexItemFixed::getIndexCode)
                .selectAs("it",QiIndexItem::getId,QiIndexItemFixed::getItemId)
                .selectAs("it",QiIndexItem::getItemCode,QiIndexItemFixed::getItemCode)
                .selectAs(QiIndexMajor::getId,QiIndexItemFixed::getMajorId)
                .selectAs(QiIndexMajor::getMajorCode,QiIndexItemFixed::getMajorCode)
                .selectAs(QiIndexMajor::getMajorName,QiIndexItemFixed::getMajorName)
                .selectAs(QiIndexMajor::getSeq,QiIndexItemFixed::getSeq)
                .leftJoin(QiIndexMain.class, "ma", p -> p.eq(QiIndexMain::getId, QiIndexMajor::getIndexId).eq(QiIndexMain::getDelFlag, 0))
                .leftJoin(QiIndexItem.class, "it", p -> p.eq(QiIndexItem::getIndexId, QiIndexMajor::getIndexId).eq(QiIndexItem::getId, param.getItemId()).eq(QiIndexItem::getDelFlag, 0))
                .leftJoin(QiIndexItemFixed.class, "fi", p -> p.eq(QiIndexItemFixed::getMajorId, QiIndexMajor::getId).eq(QiIndexItemFixed::getIndexId, QiIndexMajor::getIndexId).eq(QiIndexItemFixed::getItemId, param.getItemId()).eq(QiIndexMajor::getDelFlag, 0))
                .eq(QiIndexMajor::getIndexId,param.getIndexId())
                .eq(QiIndexMajor::getDelFlag,0);
        List<QiIndexItemFixed> qiIndexItemFixeds = qiIndexMajorMapper.selectJoinList(QiIndexItemFixed.class, queryWrapper);
        return Result.succ(1, "查询成功", qiIndexItemFixeds);

    }

    @Override
    public Result saveOrUpdateIndexItemFixed(QiIndexItemFixedDto dto) {
        List<QiIndexItemFixed> qiIndexItemFixedList = dto.getQiIndexItemFixedList();
        boolean b = this.saveOrUpdateBatch(qiIndexItemFixedList);

        if (b) {
            return Result.succ(1, "成功", null);
        } else {
            return Result.fail(400, "失败", "");
        }
    }

    @Override
    public Result batchDeleteFixedList(QiIndexItemFixedDto dto) {
        List<QiIndexItemFixed> qiIndexItemFixedList = dto.getQiIndexItemFixedList();
        List<String> ids = qiIndexItemFixedList.stream().map(QiIndexItemFixed::getId).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);

        if (b) {
            return Result.succ(1, "删除成功", null);
        } else {
            return Result.fail(400, "删除失败", "");
        }    }
}
